﻿using ConfExpert.Datasets.MyDataTableAdapters;
using ConfExpert.Forms.Base;
using ConfExpert.Forms.Rapor;
using ConfExpert.Forms.Siparisler;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using DevExpress.XtraVerticalGrid.Events;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class StokGiris : XForm
    {
        int GT;

        DataSet dsGP;
        DataTable dtGP;
        DataTable dtGPL;
        DataTable dtRoll;
        DataTable dtAyrim;

        public StokGiris(int GirisTipiId)
        {
            InitializeComponent();


            TAM.GelisPartisiTableAdapter = new GelisPartisiTableAdapter();
            TAM.GelisPartisiLinesTableAdapter = new GelisPartisiLinesTableAdapter();
            TAM.RollTableAdapter = new RollTableAdapter();
            this.GT = GirisTipiId;
        }

        private void StokGiris_Load(object sender, EventArgs e)
        {
            //gleGirisTipi.Properties.Assign(RI(Rp.HareketTipi));
            //gleGirisTipi.Properties.DataSource = new DataView(AppSession.Lookups.HareketTipi, "HareketTipiId in (18,19,20,21,8,7,5)", "Name", DataViewRowState.OriginalRows);
            //GirisTipleri = new DataView(AppSession.Lookups.HareketTipi, "HareketTipiId in (18,19,20,21,8,7,5)", "Name", DataViewRowState.OriginalRows);
            //ÖNEMLİ NOT: yeni yapıda mevcut girişler kaldırılacak! TELA için kalabilir, onun satınalma tarafı henüz yapılmadı!


            BindData();

            DataTable dt = ExecuteDataTable("GetFasonIslemAtelyeleri");
            riF = GetCustomGLE(dt, "Name", "VendorId");
            riF.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            dtAyrim.TableNewRow += dtAyrim_TableNewRow;
            gvAyrim.FocusedRowChanged += gvAyrim_FocusedRowChanged;

            GridMenu gm = GridManager.GetManager(gvGPL).GridMenu;
            gm.AddMenu("Bu artikellerin tüm toplarını ayır", AyirP);

            gvRoll.OptionsView.NewItemRowPosition = gvGPL.OptionsView.NewItemRowPosition = gvAyrim.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            AddTooltip(gvRoll, "Qty", "Top metrajı");

            gvGPL.MakeCompulsory("ProductId");
            gvRoll.MakeCompulsory("Qty", "AyrimId");

            gvRoll.MakeColumnsReadOnly("RollId", "BrandId", "AyrimId");

            //gleUretici.BorderStyle = BorderStyles.Default;  //properties.assign dan sonra kullanılması zorunlu yoksa borderless görünüyor!

            FormClosing += new FormClosingEventHandler(StokGiris_FormClosing);
            gvGPL.ShowingEditor += gvGelisPartisiLines_ShowingEditor;
            gvRoll.ShowingEditor += gvRoll_ShowingEditor;
            gvAyrim.ShowingEditor += gvAyrim_ShowingEditor;

            gm = GridManager.GetManager(gvRoll).GridMenu;
            gm.AddMenu("Seçili topları ayır", AyirR);

            sccGelenLines.AutoSize();

            gvAyrim.OptionsView.EnableAppearanceOddRow = gvAyrim.OptionsView.EnableAppearanceEvenRow = false;

            gvAyrim.RowStyle += new RowStyleEventHandler(gvAyrim_RowStyle);
            gvAyrim.RowCellStyle += new RowCellStyleEventHandler(gvAyrim_RowCellStyle);

            //gvAyrim.OptionsSelection.MultiSelect = false;
            //gvAyrim.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            //gvAyrim.Appearance.FocusedRow.BackColor = Color.YellowGreen;

            gvRoll.GetManager().RowDeleteDisabled =
                gvGPL.GetManager().RowDeleteDisabled =
                gvAyrim.GetManager().RowDeleteDisabled = false;

            CreateVerticalGrid();

            bbiAppend.ItemClick += bbiAppend_ItemClick;
            bbiSave.ItemClick += bbiSave_ItemClick;
            bbiBack.ItemClick += bbiBack_ItemClick;
            bbiPrint.ItemClick += bbiPrint_ItemClick;
            bbiKumasAra.ItemClick += bbiKumasAra_ItemClick;

            vgc.SetCellValue(vgc.Rows["GirisTipiId"], 0, GT);
            GTChanged();

            gvGPL.SetViewCaption("Gelen Kumaşlar");
            gvRoll.SetViewCaption("Toplar");
            gvAyrim.SetViewCaption("Ayrım");


        }

        void bbiKumasAra_ItemClick(object sender, ItemClickEventArgs e)
        {
            using (FuzzySearch fuzzy = new FuzzySearch())
            {
                if (fuzzy.ShowDialog() == DialogResult.OK)
                {
                    if (gvGPL.FocusedRowHandle != DevExpress.XtraGrid.GridControl.NewItemRowHandle)
                        gvGPL.SetFocusedRowCellValue("ProductId", fuzzy.ProductId);
                    else
                    {
                        if (dtGPL.Select("ProductId=" + fuzzy.ProductId).Length == 0)
                        {
                            DataRow dr = dtGPL.NewRow();
                            dr["ProductId"] = fuzzy.ProductId;
                            dtGPL.Rows.Add(dr);
                        }
                    }
                }
                gvGPL.EndEditing();
                gvGPL.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
                gvGPL.RefreshData();
                gvGPL.BestFitColumns();
            }
        }


        void gvRoll_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !(gvRoll.FocusedRowHandle == DevExpress.XtraGrid.GridControl.NewItemRowHandle ||
                gvRoll.GetFocusedRowCellValue("RollId").ToInt() < 0) ||
                gvRoll.FocusedColumn.FieldName == "AyrimId";
        }

        void bbiAppend_ItemClick(object sender, ItemClickEventArgs e)
        {
            using (OncekiPartiyeEkle ope = new OncekiPartiyeEkle())
            {
                if (ope.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Yenile();

                    DataRow dr = ope.dr;
                    GPId = dr["GelisPartisiId"].ToInt();

                    vgc.SetCellValue(vgc.Rows["GelisPartisiId"], 0, dr["GelisPartisiId"]);
                    vgc.SetCellValue(vgc.Rows["GirisTipiId"], 0, dr["GirisTipiId"]);
                    vgc.SetCellValue(vgc.Rows["AtelyeId"], 0, dr["AtelyeId"]);
                    vgc.SetCellValue(vgc.Rows["VendorId"], 0, dr["VendorId"]);
                    vgc.SetCellValue(vgc.Rows["CustomerId"], 0, dr["CustomerId"]);
                    vgc.SetCellValue(vgc.Rows["OwnerId"], 0, dr["OwnerId"]);
                    vgc.SetCellValue(vgc.Rows["BelgeTipiId"], 0, dr["BelgeTipiId"]);
                    vgc.SetCellValue(vgc.Rows["BelgeNo"], 0, dr["BelgeNo"]);
                    vgc.SetCellValue(vgc.Rows["BelgeTarihi"], 0, dr["BelgeTarihi"]);
                    vgc.SetCellValue(vgc.Rows["Notes"], 0, dr["Notes"]);
                    vgc.SetCellValue(vgc.Rows["PONr"], 0, dr["PONr"]);
                    //vgc.OptionsBehavior.Editable = false;

                    foreach (BaseRow r in vgc.Rows)
                        r.Properties.ReadOnly = !r.Name.StartsWith("Belge");

                    BindData();

                    vgc.FocusedRow = vgc.Rows["BelgeTipiId"];
                }
            }
        }

        void bbiBack_ItemClick(object sender, ItemClickEventArgs e)
        {
            Yenile();
        }

        private void Yenile()
        {
            vgc.CloseEditor();

            dtGP.Clear();
            dtGPL.Clear();
            dtRoll.Clear();
            dtAyrim.Clear();
            gvGPL.RefreshData();
            gvRoll.RefreshData();
            gvAyrim.RefreshData();

            btnTemizle_Click(null, null);
            vgc.SetCellValue(vgc.Rows["GirisTipiId"], 0, GT);
            //vgc.OptionsBehavior.Editable = true;

            foreach (BaseRow r in vgc.Rows)
                r.Properties.ReadOnly = false;

            GTChanged();
        }


        int GPId = 0;
        private void BindData()
        {
            dsGP = ExecuteDataSet("GetStokGiris", GPId);
            dtGP = dsGP.Tables[0].Copy();
            dtGP.TableName = "GP";




            dtGPL = dsGP.Tables[1];
            dtGPL.TableName = "GPL";
            dtGPL.PrimaryKey = new DataColumn[] { dtGPL.Columns["GelisPartisiLinesId"] };
            dtGPL.AddAutoIncrementalColumn("GelisPartisiLinesId");



            dtRoll = dsGP.Tables[2];
            dtRoll.TableName = "Roll";
            dtRoll.PrimaryKey = new DataColumn[] { dtRoll.Columns["RollId"] };
            dtRoll.AddAutoIncrementalColumn("RollId");



            //
            dtAyrim = dsGP.Tables[3];   //.Copy();
            dtAyrim.TableName = "Ayrim";
            dtAyrim.AddAutoIncrementalColumn("AyrimId");

            DataRelation drlx = new DataRelation("FK_Ayrim_Roll_AyrimId", dtAyrim.Columns["AyrimId"], dtRoll.Columns["AyrimId"]);
            if (!dtAyrim.ChildRelations.Contains("FK_Ayrim_Roll_AyrimId"))
                dtAyrim.ChildRelations.Add(drlx);

            dtRoll.Columns.Add("BrandId", typeof(int), "Parent(FK_Ayrim_Roll_AyrimId).BrandId");
            dtRoll.Columns.Add("OrderRef", typeof(string), "Parent(FK_Ayrim_Roll_AyrimId).OrderRef");

            gvAyrim.Init(dtAyrim);
            gvAyrim.MakeColumnInvisible("AyrimId");
            //





            DataRelation drl = new DataRelation("FK_GPL_Roll_GelisPartisiLinesId", dtGPL.Columns["GelisPartisiLinesId"], dtRoll.Columns["GelisPartisiLinesId"]);
            if (!dtGPL.ChildRelations.Contains("FK_GPL_Roll_GelisPartisiLinesId"))
                dtGPL.ChildRelations.Add(drl);

            dtGPL.Columns["Qty"].Expression = "IsNull(SUM(Child(FK_GPL_Roll_GelisPartisiLinesId).Qty), 0)";

            BindingSource bsMaster = new BindingSource(dsGP, "GPL");
            BindingSource bsDetail = new BindingSource(bsMaster, "FK_GPL_Roll_GelisPartisiLinesId");

            InitGridView(gvGPL, bsMaster);
            InitGridView(gvRoll, bsDetail);







            gvGPL.MakeColumnInvisible("GelisPartisiLinesId");
            gvGPL.SetCaptions("VendorId:Üretici");
            gvGPL.YanYanaGetir("Qty:VendorId");
            gvGPL.FormatAsNumber(1, "Qty");
            gvGPL.AddSummary("Qty");

            gvRoll.SetCaptions("Qty:Metraj");
            gvRoll.YanYanaGetirSoluna("AyrimId", "BrandId", "OrderRef");
            gvRoll.MakeColumnInvisible("GelisPartisiLinesId");
            gvRoll.FormatAsNumber(1, "Qty");
            gvRoll.AddSummary("Qty");

            GTChanged();

            gvGPL.BestFitColumns();
            gvRoll.BestFitColumns();
            gvAyrim.BestFitColumns();

        }

        void gvAyrim_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gvAyrim.BestFitColumns();
        }

        void dtAyrim_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            switch (intParse(GetVal("GirisTipiId")))
            {
                case 20:    //GirisTipi.Du4
                    e.Row["BrandId"] = 47; //Du4
                    break;

                case 21:    //GirisTipi.Tela
                    e.Row["BrandId"] = 1;
                    break;
            }
        }

        void bbiSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            vgc.CloseEditor();
            gvGPL.EndEditing();
            gvRoll.EndEditing();

            int focusedHandle = gvGPL.FocusedRowHandle;
            int focusedColumnHandle = gvGPL.FocusedColumn.AbsoluteIndex;

            if (dtGPL.Rows.Count == 0)
                throw new ApplicationException("Lütfen en az bir satıra kod yazın.");

            if (
                checkEmpty(dtRoll, "Qty") ||
                checkEmpty(dtRoll, "AyrimId") ||
                intParse(GetVal("OwnerId")) == 0
                )
            {
                XtraMessageBox.Show("Zorunlu alanları doldurduktan sonra tekrar deneyin", "Eksik giriş", MessageBoxButtons.OK);
                return;
            }

            dtGP.Rows.Clear();
            DataRow drGP = dtGP.NewRow();
            drGP["GelisPartisiId"] = GetVal("GelisPartisiId");
            drGP["GirisTipiId"] = GetVal("GirisTipiId");
            drGP["AtelyeId"] = GetVal("AtelyeId");
            drGP["VendorId"] = GetVal("VendorId");
            drGP["CustomerId"] = GetVal("CustomerId");
            drGP["OwnerId"] = GetVal("OwnerId");
            drGP["BelgeTipiId"] = GetVal("BelgeTipiId");
            drGP["BelgeNo"] = GetVal("BelgeNo");
            drGP["BelgeTarihi"] = GetVal("BelgeTarihi");
            drGP["Notes"] = GetVal("Notes");
            drGP["PONr"] = GetVal("PONr");
            dtGP.Rows.Add(drGP);

            try
            {
                DataTable result = ExecuteDataTable("InsStokGirisManuel",
                    dtGP.Serialize(),
                    dtGPL.Serialize(),
                    dtRoll.Serialize(),
                    dtAyrim.Serialize(),
                    WhoAmI);

                GPId = result.Rows[0][0].ToInt(-1);

                vgc.SetCellValue(vgc.Rows["GelisPartisiId"], 0, GPId);
                Notify("Kaydedildi");
            }
            catch (Exception ex)
            {
                if (ex.ToString().ToLower().Contains("uc_po"))
                    throw new ApplicationException("Aynı artikel birden çok satırda geçiyor. Bir artikel bir satırda geçmelidir.", ex);
                else
                    throw ex;
            }

            BindData();

            AppSession.MainForm.NotifyMain("Stok, " + GPId + " parti no. ile kaydedildi");

            gvGPL.FocusedRowHandle = focusedHandle;
            gvGPL.FocusedColumn.AbsoluteIndex = focusedColumnHandle;
            gvGPL.EndEditing();
        }

        RepositoryItemGridLookUpEdit riGT, riF;
        private void CreateVerticalGrid()
        {
            DataTable dtGT = AppSession.Lookups.HareketTipi
                                    .DefaultView.ToTable(false, "HareketTipiId", "Name")
                                    .Select("HareketTipiId in (" + (ytk(136) ? "18," : string.Empty) + "21,5,7,8,19)", "Name")
                                    .CopyToDataTable(); /*18,19,8,7,5,29*/
            riGT = RepositoryManager.GetGLESablon(dtGT, "Name", "HareketTipiId");

            //DataTable dtF = AppSession.Lookups.Vendor
            //            .DefaultView.ToTable(false, "VendorId", "Name")
            //            .Select("VendorId in (5,9,110,121,177,290,572,583)", "Name")
            //            .CopyToDataTable();
            //riF = RepositoryManager.GetGLESablon(dtF, "Name", "VendorId");


            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("GirisTipiId", typeof(int),riGT),    //gereksizlerin silindi = 1 yap!
                AddItem("GelisPartisiId", typeof(int)),
                AddItem("PONr", typeof(string)),
                AddItem("OwnerId", typeof(int), RI(Rp.V_Customer)),
                AddItem("VendorId", typeof(int), RI(Rp.Vendor)),
                AddItem("AtelyeId", typeof(int), RI(Rp.Atelye)),
                AddItem("CustomerId", typeof(int), RI(Rp.V_Customer)),
                AddItem("BelgeTipiId", typeof(int), RI(Rp.BelgeTipiAlis)),
                AddItem("BelgeTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
                AddItem("BelgeNo", typeof(string)),
                AddItem("Notes", typeof(string)),
            });
            vgc.Rows.GetRowByFieldName("Notes", true).Height = 80;
            vgc.Rows["OwnerId"].Properties.Caption = "Mal Sahibi";
            vgc.Rows["VendorId"].Properties.Caption = "Üretici";
            vgc.Rows["GelisPartisiId"].Properties.ReadOnly = true;
            vgc.Rows["GirisTipiId"].Properties.RowEdit.EditValueChanged += RowEdit_EditValueChanged;

            splitContainerControl2.SplitterPosition = vgc.Height - 20;

            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            //if (BelgeNo != string.Empty)
            //{
            //    vgc.SetCellValue(vgc.Rows["BelgeNo"], 0, BelgeNo);
            //    vgc.SetCellValue(vgc.Rows["BelgeTipiId"], 0, BelgeTipiId);
            //    vgc.SetCellValue(vgc.Rows["VendorId"], 0, UreticiId);

            //}

            vgc.FocusedRow = vgc.Rows["GirisTipiId"];
            vgc.BestFit();
        }

        void RowEdit_EditValueChanged(object sender, EventArgs e)
        {
            GTChanged();
        }

        private void GTChanged()
        {
            vgc.CloseEditor();

            int girisTipiId = GetVal("GirisTipiId").ToInt();
            gvRoll.OptionsBehavior.Editable = gvGPL.OptionsBehavior.Editable = girisTipiId > 0;
            gvGPL.OptionsView.NewItemRowPosition = girisTipiId == 29 ? NewItemRowPosition.None : NewItemRowPosition.Bottom;

            switch (girisTipiId)
            {
                case 21:    //GirisTipi.Tela:
                    bbiKumasAra.Visibility = BarItemVisibility.Never;
                    vgc.SetCellValue(vgc.Rows["OwnerId"], 0, 1);
                    gvGPL.ShowProductIdAsTela();
                    break;

                default:
                    gvGPL.ShowProductIdAsKumas();
                    break;
            }

            switch (girisTipiId)
            {
                case 8:
                    vgc.Rows["VendorId"].Properties.RowEdit = riF;

                    vgc.Rows["VendorId"].Visible = true;
                    vgc.Rows["AtelyeId"].Visible = false;
                    vgc.Rows["CustomerId"].Visible = false;
                    break;

                case 4:
                case 18:
                case 20:
                case 21:
                case 29:
                    vgc.Rows["VendorId"].Properties.RowEdit = RI(Rp.Vendor);

                    vgc.Rows["VendorId"].Visible = true;
                    vgc.Rows["AtelyeId"].Visible = false;
                    vgc.Rows["CustomerId"].Visible = false;
                    break;

                case 5:
                    vgc.Rows["VendorId"].Visible = false;
                    vgc.Rows["AtelyeId"].Visible = true;
                    vgc.Rows["CustomerId"].Visible = false;
                    break;

                case 7:
                case 19:
                    vgc.Rows["VendorId"].Visible = false;
                    vgc.Rows["AtelyeId"].Visible = false;
                    vgc.Rows["CustomerId"].Visible = true;
                    break;
            }


            if (girisTipiId > 0)
                GT = girisTipiId;

            if (girisTipiId == 29)
                splitContainerControl2.PanelVisibility = SplitPanelVisibility.Panel1;
            else
                splitContainerControl2.PanelVisibility = SplitPanelVisibility.Both;

            vgc.Refresh();
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            object o = vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
            if (o != null)
                return o;
            else
                return DBNull.Value;
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
            foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);
        }

        void gvAyrim_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            switch (e.Column.FieldName)
            {
                case "BrandId":
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    break;

                //case "AyrimId":
                //    if (gvAyrim.GetFocusedRowCellValue("AyrimId") == gvRoll.GetFocusedRowCellValue("AyrimId"))
                //        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                //        //e.Appearance.BackColor = e.Appearance.BackColor2 = Color.LemonChiffon;   // Color.FromArgb(255, 192, 0);
                //    break;
            }
        }



        void gvAyrim_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
            {
                if (e.RowHandle == view.FocusedRowHandle)
                    e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);
            }
        }

        void AyirP(object sender, EventArgs e)
        {
            gvGPL.EndEditing();
            gvAyrim.EndEditing();
            int[] selectedRows = gvGPL.GetSelectedRows();

            int AyrimId = intParse(gvAyrim.GetFocusedRowCellValue("AyrimId"));

            if (AyrimId != 0)
                foreach (int handle in selectedRows)
                    foreach (DataRow dr in dtRoll.Select("GelisPartisiLinesId=" + gvGPL.GetRowCellValue(handle, "GelisPartisiLinesId").ToString(), string.Empty))
                        if (dr["RollId"].ToInt() < 0)
                            dtRoll.Rows.Find(intParse(dr["RollId"]))["AyrimId"] = AyrimId;

            gvRoll.RefreshData();

            foreach (int handle in gvGPL.GetSelectedRows())
                gvGPL.UnselectRow(handle);

            foreach (int handle in selectedRows)
                gvGPL.SelectRow(handle);
        }

        void AyirR(object sender, EventArgs e)
        {
            gvRoll.EndEditing();
            gvAyrim.EndEditing();
            int[] selectedRows = gvRoll.GetSelectedRows();

            int AyrimId = intParse(gvAyrim.GetFocusedRowCellValue("AyrimId"));

            if (AyrimId != 0)
                foreach (int handle in selectedRows)
                    if (gvRoll.GetRowCellValue(handle, "RollId").ToInt() < 0)
                        dtRoll.Rows.Find(intParse(gvRoll.GetRowCellValue(handle, "RollId")))["AyrimId"] = AyrimId;

            gvRoll.RefreshData();

            foreach (int handle in gvRoll.GetSelectedRows())
                gvRoll.UnselectRow(handle);

            foreach (int handle in selectedRows)
                gvRoll.SelectRow(handle);
        }

        private void gvGelisPartisiLines_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (gvGPL.FocusedColumn.FieldName == "VendorId" ||
                gvGPL.FocusedColumn.FieldName == "UreticiKodu")
            {
                e.Cancel = true;
                int ProductId = gvGPL.GetFocusedRowCellValue("ProductId").ToInt();
                if (ProductId > 0)
                {
                    using (Kodlar kodlar = new Kodlar(ProductId, true))
                    {
                        if (kodlar.ShowDialog() == DialogResult.OK)
                        {
                            gvGPL.SetFocusedRowCellValue("VendorId", kodlar.VendorId);
                            gvGPL.SetFocusedRowCellValue("UreticiKodu", kodlar.UreticiKodu);
                        }
                    }
                }
            }
            else
                e.Cancel = !(gvGPL.FocusedRowHandle == DevExpress.XtraGrid.GridControl.NewItemRowHandle ||
                    gvGPL.GetFocusedRowCellValue("GelisPartisiLinesId").ToInt() < 0);


            //gvRoll.FocusedRowHandle = gvRoll.RowCount - 1;
            //gvRoll.FocusedColumn = gvRoll.Columns["qty"];
            //gvRoll.ShowEditor();
        }

        private void gvAyrim_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !(gvAyrim.FocusedRowHandle == DevExpress.XtraGrid.GridControl.NewItemRowHandle ||
                gvAyrim.GetFocusedRowCellValue("AyrimId").ToInt() < 0);
        }



        private void StokGiris_FormClosing(object sender, FormClosingEventArgs e)
        {
            bool res = false;
            if (dtGPL.HasChanged())
                res = XtraMessageBox.Show("Emin misiniz?", "Uyarı", MessageBoxButtons.YesNo) != DialogResult.Yes;
            e.Cancel = res;
        }

        void bbiPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraReport barcode;

            switch (intParse(GetVal("GirisTipiId")))
            {
                case 21:    //GirisTipi.Tela:
                    barcode = new Rapor.TelaBarkod(Enums.EtiketTipi.GelisPartisiId, GPId.ToString());
                    barcode.ShowPreviewDialog();
                    break;

                default:
                    TopEtiketleri2 te = new TopEtiketleri2(Enums.EtiketTipi.GelisPartisiId, GPId.ToString());
                    te.ShowDialog();
                    break;
            }

            //reportPresenter.View(barcode);
        }

    }
}
